//index.js
//获取应用实例
const app = getApp()
wx.cloud.init();
var checkResult = function (result) {
  if (!result || result.code !== 20000) {
    console.log('error' + result.code);
    wx.showToast({
      icon: 'none',
      title: '没有数据或者操作失败，请刷新尝试',
    });
    return false;
  }
  return true;
};
Page({
  data: {
  },
  translateState: function (s) {
    if (!s) {
      return '未知'
    } else if (s === 'open') {
      return '开放';
    } else if (s === 'close') {
      return '关闭';
    } else {
      return '过期';
    }
  },
  onLoad: function (options) {
    console.log('Reloading...', options);
    var that = this;
    if (!options.area) {
      wx.redirectTo({
        url: '../index/index',
      });
      return;
    }
    var area = options.area;
    this.setData({
      area: area
    });
    console.log('user role from global: ', app.globalData.userrole);
    wx.cloud.callFunction({
      name: 'admin',
      data: {
        action: 'contests',
        userrole: app.globalData.userrole || '',
        area: area
      },
      success: function (res) {
        console.log(res);
        var result = res.result;
        if (!checkResult(result)) {
          return;
        }
        var list = result.data;
        var contests = [];
        var oldcontests = [];
        list.forEach(function (item) {
          item.stateword = that.translateState(item.state);
          if (item.state === 'open') {
            contests.push(item);
          } else {
            oldcontests.push(item);
          }
        });
        that.setData({
          contests: contests,
          oldcontests: oldcontests
        });
        if(area === 'host'){
          //如果是管理员和vip可以创建比赛
          if(app.globalData.userrole === 'admin' || app.globalData.userrole === 'vip'){
            that.setData({
              showAdd: true
            });
          } else if(app.globalData.userrole && list.length === 0){
            //其他角色如果还没有创建比赛，可以创建
            that.setData({
              showAdd: true
            });
          }
        }
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          icon: 'none',
          title: '加载失败，请尝试刷新',
          duration: 2000
        });
      }
    });
  },
  addContest: function(){
    wx.navigateTo({
      url: '../contest-edit/contest-edit',
    });
  },
  gotoContest: function (e) {
    var sid = e.currentTarget.dataset.sid;
    if (!sid) {
      console.log('contest sid is empty');
      return;
    }
    if (this.data.area === 'host') {
      if (!app.globalData.userrole) {
        wx.navigateTo({
          url: '../control/control?sid=' + sid + '&guest=true',
        });
      } else {
        wx.navigateTo({
          url: '../control/control?sid=' + sid,
        });
      }
    } else {
      if (!app.globalData.userrole) {
        wx.navigateTo({
          url: '../judge/judge?sid=' + sid + '&guest=true',
        });
      } else {
        wx.navigateTo({
          url: '../judge/judge?sid=' + sid,
        });
      }
    }


  },
  onShareAppMessage: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  },
  onShareTimeline: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  }

})
